package com.yx.cloud.product.gyro.bean;

import android.util.SparseIntArray;
import com.iot.product.sweeper.bean.MapInfo;
import com.iot.common.utils.LogUtils;
import java.util.Arrays;

public class GyroMapInfo extends MapInfo {
    public SparseIntArray mapArray = new SparseIntArray();
    public static boolean needFlip;
    public int maxWidth = 255;
    public int maxHeight = 255;

    public int left = 127;
    public int right = 127;
    public int top = 127;
    public int bottom = 127;

    public int cleanArea;
    public int cleanTime;

    public SparseIntArray getMapArray() {
        return mapArray;
    }

    public void setMapArray(SparseIntArray mapArray) {
        this.mapArray = mapArray;
    }

    public GyroMapInfo(){
        clear();
    }
    @Override
    public void clear() {
        LogUtils.i("clear rs map");
        super.clear();
        mapArray.clear();
        maxWidth = 255;
        maxHeight = 255;
        left = 127;
        right = 127;
        top = 127;
        bottom = 127;
    }

    /*
    00，当前点
    01，障碍点
    02，已清扫点
    03，充电桩
    04， （预留）*/
    public void convertArrayToMap() {
        LogUtils.i("maxwidth: " + maxWidth + " maxheight: " + maxHeight+"  width: " + width + " height: " + height);
        int[] data = new int[width * height];

        //先全部填充为空
        Arrays.fill(data, TYPE_NONE);

        if (needFlip) {
            for (int i = 0; i < mapArray.size(); i++) {
                int key = mapArray.keyAt(i);
                int x = (key >> 8) & 0xFF;
                int y = (maxWidth - key & 0xFF);
                int t = mapArray.get(key);
                //不是墙的全填成地板
                if (t == 1) {
                    data[y * maxWidth + x] = TYPE_WALL;
                } else {
                    data[y * maxWidth + x] = TYPE_ORIGIN;
                }
            }
        } else {
            for (int i = 0; i < mapArray.size(); i++) {
                int key = mapArray.keyAt(i);
                int x = (key >> 8) & 0xFF;
                int y = key & 0xFF;
                int t = mapArray.get(key);

                if (t == 1) {
                    data[y * maxWidth + x] = TYPE_WALL;
                } else {
                    data[y * maxWidth + x] = TYPE_ORIGIN;
                }
            }
        }
        mapData = data;
    }

    public void calculateMapRange() {
        int minX = 127;
        int maxX = 127;
        int minY = 127;
        int maxY = 127;
        if (mapArray != null && mapArray.size() > 0) {
            int key = mapArray.keyAt(0);
            int x = (key >> 8) & 0xFF;
            int y = key & 0xFF;
            minX = x;
            maxX = x;
            minY = y;
            maxY = y;
        }
        //设置x y的最大跨度 需要根据最大跨度缩放
        for (int i = 0; i < mapArray.size(); i++) {
            int key = mapArray.keyAt(i);
            int x = (key >> 8) & 0xFF;
            int y = (maxWidth - key & 0xFF);

            if (x < minX) {
                minX = x;
            }
            if (x > maxX) {
                maxX = x;
            }

            if (y < minY) {
                minY = y;
            }

            if (y > maxY) {
                maxY = y;
            }
        }
        width = maxX - minX+1;
        height = maxY - minY+1;
        LogUtils.i(" left :"+ minX+ " top: "+ minY + "  right: "+maxX+ " bottom: "+maxY);
    }
}
